var faults=[];//所有故障
var voltage="all";
var tid;
//{typhoonid,faultDTOs}
var typhoonFaultMap=new Map();
//所有有故障的台风点，方便点击柱状图显示tb
var typhoons=[];
$(function(){
    tid = $('#tid').val();
	getData();
});
//换电压等级
function onVolChange(){
	typhoonFaultMap.clear();
	typhoons=[];
	voltage=$("#volSelector").val();
	destoryFaultTb();
	//destoryFaultDetailTb();
	getData();
}
//根据台风id获取相关故障
function getData() {
	if(tid){
		$.getJSON('/rest/TyphoonRestService/getFaultByTyphoonId/' + tid+"/"+voltage,
				function(result) {
			faults = result;
			getChartDatas();
		});
	}
}

function getChartDatas(){
//{id,typhoon}
	var typhoonMap=new Map();
	var tyTime=[];
	for(var i in faults){
		var typhoon=faults[i].historyTyphoon;
		if(typhoon){
			var faultList=typhoonFaultMap.get(typhoon.id);
			if(!faultList){
				faultList=[];
			}
			faultList.push(faults[i]);
			typhoonFaultMap.put(typhoon.id,faultList);
			typhoonMap.put(typhoon.id,typhoon);
		}
	}
	for(var i in typhoonMap.keys){
		var typhoonId=typhoonMap.keys[i];
		var typhoon=typhoonMap.get(typhoonId);
		typhoons.push(typhoon);
		var time=getDayHourTime(typhoon['updateTime']);
		var faultArr=typhoonFaultMap.get(typhoonId);
		var tylevelCount = faultArr.length;
		tyTime.push({"time":time,"count":tylevelCount});
	}
	var maxTime="";
	var maxValue=0;
	var xAxisData = [];
	var yAxisData =[]; 
	for(var i in tyTime){
		xAxisData.push(tyTime[i].time);
		yAxisData.push(tyTime[i].count);
		if(tyTime[i].count>maxValue){
			maxValue=tyTime[i].count;
			maxTime=tyTime[i].time;
		}
	}
	drawChart(xAxisData,yAxisData);
	showTyphoonFault(maxTime,maxValue);
}
var getDayHourTime=function(t){
	var milSeconds=t.time;
	var pmilSeconds=milSeconds-3600*1000;
	var cTime=DateUtil.pattern(t,'dd日hh时');
	var pTime=DateUtil.pattern(pmilSeconds,'dd日hh时');
	if(cTime.split("日")[0]==pTime.split("日")[0]){
		cTime=cTime.split("日")[1];
	}
	return pTime+"-"+cTime;
}
function showTyphoonFault(maxTime,maxValue) {
	var voltages = {'all':'','499':'500kV及以上','220':'220kV','110':'110kV'};
	var str="本次台风共发生"+voltages[voltage]+"故障<span class='count'>"+ faults.length + "</span>次";
	if(maxTime!=""&&maxValue>0){
		str+="，其中"+maxTime+"故障发生次数最多为<span class='count'>"+maxValue+"</span>次。"
	}
	$('#typhoonTitle').html(str);
}
var bar;
var selectedTyphoonId;//点击的柱子对应的台风记录id
function drawChart(xAxisData,yAxisData){
	bar = new CategoryBar("bar");
	bar.setxAxis(xAxisData);
	bar.setyAxis(yAxisData);
	bar.setxAxisTitle("时间");
	bar.setyAxisTitle("故障次数");
	bar.setyAxisName("故障次数");
	var option = bar.getOption();
	option.xAxis[0].axisLabel.rotate='-45';
	option.xAxis[0].axisLabel.interval='0';
	bar.show();
	bar.getChart().on("click",function(param){
		//取到相关故障，组织数据
		var dataIndex=param.dataIndex;
		selectedTyphoonId=typhoons[dataIndex].id;
		var time=xAxisData[dataIndex];
		//destoryFaultDetailTb();
		initFaultTb(time);
	});
}
//点击柱子显示相应的故障信息
var faultTb;
function initFaultTb(time){
	$("#tbTitle").html(time+"故障");
	var fault=typhoonFaultMap.get(selectedTyphoonId);
	//{name,count}
	var faultMap=new Map();
	for(var i in fault){
		var eName=fault[i].faultRecord.equipment;
		var count=faultMap.get(eName);
		if(!count){
			count=0;
		}
		count++;
		faultMap.put(eName,count);
	}
	var data=[];
	for(var i in faultMap.keys){
		var name=faultMap.keys[i];
		data.push([name,faultMap.get(name)]);
	}
	data.sort(function(x,y){
		return y[1]-x[1];
	});
	if(!faultTb){
		var opt = MyTable.getDefaultOptions();
		opt.paging = false;
		opt.scrollY = '254px';
		var columns = [ '故障设备', '故障次数'];
		faultTb = new MyTable('faultTb', columns, opt);
		faultTb.setDataSet(data);
		faultTb.show();
		//faultTb.onRowClick(genFaultDetailTbData);
	}else{
		faultTb.reload(data);
	}
}
//点击次数表格显示相应的故障信息
/*function genFaultDetailTbData(selectedData,event,index){
	var fault=typhoonFaultMap.get(selectedTyphoonId);
	var equipment=selectedData[0];
	var data=[];
	for(var i in fault){
		var faultRecord=fault[i].faultRecord;
		if(equipment==faultRecord.equipment){
			var lineid = faultRecord['equipment_id'];
			var ftime = time(faultRecord.faultTime);
			var faultReport = '<button class="btn btn-sm btn-primary fa fa-search" onclick="showFaultReport(\'' + lineid
			+ '\',\'' + ftime + '\')"></button>';
			data.push([faultRecord.rank,time(faultRecord.faultTime),faultRecord.description,faultRecord.ranging,faultRecord.remark,getRadius(fault[i].radius),faultReport]);
		}
	}
	initFaultDetailTb(equipment,data);
}
//详细表格
var faultDetailTb;
function initFaultDetailTb(equipment,data){
	$("#faultDetailTbTitle").html(equipment);
	if(!faultDetailTb){
		var opt = MyTable.getDefaultOptions();
		opt.paging = false;
		opt.scrollY = '260px';
		opt.columnDefs = [ {
			"width" : "15%",
			"targets" : [ 1, 4]
		},{
			"width" : "8%",
			"targets" : [ 0,5,6]
		}, {
			"width" : "26%",
			"targets" : [2]
		}, {
			"width" : "20%",
			"targets" : [3]
		} ];
		
		var columns = [ '调管范围', '故障时间',"故障过程","行波测距","备注","风圈","详细"];
		faultDetailTb = new MyTable('faultDetailTb', columns, opt);
		faultDetailTb.setDataSet(data);
		faultDetailTb.show();
	}else{
		faultDetailTb.reload(data);
	}
}
function getRadius(radius){
	var result=radius;
	if(radius=="0"){
		result="小于7级";
	}else if(radius=="-1"){
		result="无地理信息";
	}
	return result;
}
function showFaultReport(lineid, faultTime) {
	var fra = document.createElement("iframe");
	fra.style = "background:transparent;border:0;";
	fra.width = "100%";
	fra.id = "faultReportIframe";
	var height=$("#mainPage").height()+390;
	fra.height = height+"px";
	var s = "/ui/map/protectActSvg/protectActSvg.xhtml?lineId=" + lineid + "&faultTime=" + faultTime
			+ "&showBackButton=" + "true";
	fra.src = s;
	document.body.appendChild(fra);
	$('#mainPage').css('display', 'none');
};
function backFromFaultReport() {
	document.body.removeChild(document.getElementById("faultReportIframe"));
	$('#mainPage').css('display', 'block');
}
function destoryFaultDetailTb(){
	$("#faultDetailTbTitle").html("");
	if(faultDetailTb){
		faultDetailTb.getTable().destroy();
		$('#faultDetailTb').empty();
		faultDetailTb=null;
	}
}*/
function destoryFaultTb(){
	$("#tbTitle").html("");
	if(faultTb){
		faultTb.getTable().destroy();
		$('#faultTb').empty();
		faultTb=null;
	}
}
function time(v) {
	if (!v) {
		return '';
	} else {
		var t = new Date(v.time ? v.time : v);
		var m = t.getMonth() + 1 > 9 ? (t.getMonth() + 1) : ("0" + (t.getMonth() + 1));
		var d = t.getDate() > 9 ? t.getDate() : "0" + t.getDate();
		var h = t.getHours() > 9 ? t.getHours() : "0" + t.getHours();
		var min = t.getMinutes() > 9 ? t.getMinutes() : "0" + t.getMinutes();
		var s = t.getSeconds() > 9 ? t.getSeconds() : "0" + t.getSeconds();
		var timestr = t.getFullYear() + "-" + m + "-" + d + " " + h + ":" + min + ":" + s;
		return timestr;
	}
}
function resizeHtml() {
	if (bar) {
		bar.getChart().resize();
	}
	if (faultTb) {
		faultTb.getTable().draw();
	}
	if (faultDetailTb) {
		faultDetailTb.getTable().draw();
	}
}